package com.itheima.controller;


import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.constant.MessageConstant;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.entity.Result;
import com.itheima.pojo.Reos;
import com.itheima.pojo.Sprots;

import com.itheima.service.SprotsService;
import com.itheima.utils.POIUtils;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
/**
 * 运动库管理
 */
@RestController
@RequestMapping("/sprot")
public class SprotsController {

    @Reference
    private SprotsService sprotsService;

    //文件上传，并保存到数据库
    @RequestMapping("/upload")
    public Result upload(@RequestParam("excelFile") MultipartFile excelFile){
        //调用poi的工具类，读取文件
        try {
            List<String[]> list = POIUtils.readExcel(excelFile);
            List<Sprots> sprotsArrayList = new ArrayList<>();
            //遍历工作表
            //健壮性判断
            if (list !=null  && list.size()>0) {
                //得到每一个行的数据
                for (String[] strings : list) {
                    System.out.println(strings.length);
                    Sprots sprots = new Sprots(strings[0],strings[1],strings[2],strings[3]);
                    //将每一个单元格对象添加到集合中
                    sprotsArrayList.add(sprots);
                    System.out.println(sprotsArrayList);
                }
                //调用service进行添加到数据库中
                sprotsService.add(sprotsArrayList);
            }
        } catch (IOException e) {
            e.printStackTrace();
            //上传失败
            return new Result(false, MessageConstant.IMPORT_ORDERSETTING_FAIL);
        }
        //上传成功
        return new Result(true, MessageConstant.IMPORT_ORDERSETTING_SUCCESS);
    }

    //分页查询
    @RequestMapping("/findpage")
    public PageResult findpage(@RequestBody QueryPageBean queryPageBean){
        PageResult pageResult=sprotsService.findpageRepos(queryPageBean);
        return pageResult;
    }
}
